package com.infodms.dms.actions.sales.marketmanage.planissued;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.actions.common.FileUploadManager;
import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.common.ErrorCodeConstant;
import com.infodms.dms.common.materialManager.MaterialGroupManagerDao;
import com.infodms.dms.dao.sales.marketmanage.plancommon.ActivitiesCommonDao;
import com.infodms.dms.dao.sales.marketmanage.planissued.ActivitiesSummaryIssuedDao;
import com.infodms.dms.dao.sales.marketmanage.planmanage.ActivitiesPlanManageDao;
import com.infodms.dms.exception.BizException;
import com.infodms.dms.po.TtCampaignExecutePO;
import com.infodms.dms.po.TtCampaignPoseChkRPO;
import com.infodms.dms.po.TtCampaignSummeryChkPO;
import com.infodms.dms.util.CommonUtils;
import com.infodms.dms.util.sequenceUitl.SequenceManager;
import com.infoservice.mvc.context.ActionContext;
import com.infoservice.mvc.context.RequestWrapper;
import com.infoservice.po3.bean.PageResult;

public class ActivitiesSummaryAreaCheck {
	
	public Logger logger = Logger.getLogger(ActivitiesSummaryAreaCheck.class);   
	ActivitiesSummaryIssuedDao dao  = ActivitiesSummaryIssuedDao.getInstance();
	private ActionContext act = ActionContext.getContext();
	
	private final String initUrl = "/jsp/sales/marketmanage/planissued/activitiesSummaryAreaCheck.jsp";
	private final String addUrl = "/jsp/sales/marketmanage/planissued/activitiesSummaryAreaCheckDetial.jsp";
	
	/**
	 * 活动执行方案提报页面初始化
	 */
	public void activitiesSummaryAreaCheckQueryInit(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getPoseIdBusiness(logonUser.getPoseId().toString());
			act.setOutData("areaList", areaList);
			act.setForword(initUrl);
		}catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"活动方案提报页面初始化");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**
	 * 活动执行方案提报查询
	 */
	public void activitiesSummaryAreaCheckQuery(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			Long poseId = logonUser.getPoseId() ;
			String areaId=request.getParamValue("areaId");
			String campaignNo = request.getParamValue("campaignNo");			//方案编号
			String campaignName = request.getParamValue("campaignName");		//方案名称
			String campaignSubject = request.getParamValue("campaignSubject");	//活动主题
			String campaignType = request.getParamValue("campaignType");		//活动类别
			String startDate = request.getParamValue("startDate");				//开始时间
			String endDate = request.getParamValue("endDate");					//结束时间
			String dealerCode=request.getParamValue("dealerCode");
			String orgId=logonUser.getOrgId().toString();
			
			TtCampaignPoseChkRPO tcpcrp = new TtCampaignPoseChkRPO();
			tcpcrp.setPoseId(poseId);			
			List list = dao.select(tcpcrp);
			if(!CommonUtils.isNullList(list)){
				tcpcrp = (TtCampaignPoseChkRPO)dao.select(tcpcrp).get(0);
			}else {
				throw new RuntimeException("您没有审核权限，请联系系统管理员！");
			}
			
			Integer curPage = request.getParamValue("curPage") !=null ? Integer.parseInt(request.getParamValue("curPage")) : 1;
			PageResult<Map<String, Object>> ps= dao.areaActivitiesSummaryIssuedQuery(poseId, areaId,orgId,campaignNo, campaignName, campaignSubject, campaignType, startDate, endDate, dealerCode, curPage, Constant.PAGE_SIZE);
			act.setOutData("ps", ps);
		} catch (RuntimeException e) {
			logger.error(logonUser,e);
			act.setException(e);
		} catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"活动执行方案提报查询");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	/**
	 * 活动总结提报页面初始化
	 */
	public void showActivitiesSummarySubCheckDetial(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		ActivitiesCommonDao cdao=ActivitiesCommonDao.getInstance();
		try {
			RequestWrapper request = act.getRequest();
			String campaignId=request.getParamValue("campaignId")==null?"":request.getParamValue("campaignId");
			String executeId=request.getParamValue("executeId")==null?"":request.getParamValue("executeId");
			//参数MAP
			Map<String, Object> conMap=new HashMap<String, Object>();
			conMap.put("campaignId", campaignId);
			conMap.put("executeId", executeId);
			conMap.put("costId", "");
			conMap.put("targetId", "");
			
			ActivitiesPlanManageDao dddao = ActivitiesPlanManageDao.getInstance();
			String modelName = dddao.getModelName(campaignId);
			//查品牌
			Map<String, Object> map = dao.selectPinPai();
			String groupName = map.get("GROUP_NAME").toString();
			//查业务范围
			List<Map<String, Object>> areaList = MaterialGroupManagerDao.getDealerBusiness(logonUser.getPoseId().toString());
			//方案详细信息
			Map<String, Object> planMap=cdao.selectCampaignDetial(executeId);
			String dealerIds=planMap.get("DEALER_ID").toString();
			String areaId=planMap.get("AREA_ID").toString();
			planMap.put("PROD_NAME", modelName);
			//附件列表
			List<Map<String, Object>> attachmentList=cdao.getAttachInfo(executeId);
			// 媒体投放费用
			List<Map<String, Object>> mediaList=cdao.selectMediaCost(conMap);
			// 市场活动费用
			List<Map<String, Object>> capList=cdao.selectCampaignCost(conMap);
			// 活动总结
			Map<String, Object> targetMap=cdao.selectCampaignTarget(conMap);
			//审批信息
			List<Map<String, Object>> checkList = cdao.getCheckInfo(executeId,campaignId);
			List<Map<String, Object>> mycheckList = cdao.getMyCheckInfo(executeId,campaignId);
			Long orgId=logonUser.getOrgId();
			List<Map<String, Object>> amountList1 = dao.queryDealerCost(
					dealerIds, areaId);	//经销商推广费用
			List<Map<String, Object>> amountList2 = dao.queryAreaCost(
					dealerIds, areaId);	//区域推广费用
			List<Map<String, Object>> amountList3 = dao.queryOemCost(orgId,
					areaId, Constant.COST_TYPE_01);	//总部市场推广费用
			List<Map<String, Object>> amountList4 = dao.queryOemCost(orgId,
					areaId, Constant.COST_TYPE_02);	//总部广告费用
			if (amountList1.size() > 0) {
				act.setOutData("amountList1", amountList1.get(0).get(
						"AVAILABLE_AMOUNT"));
			}
			if (amountList2.size() > 0) {
				act.setOutData("amountList2", amountList2.get(0).get(
						"AVAILABLE_AMOUNT"));
			}
			if (amountList3.size() > 0) {
				act.setOutData("amountList3", amountList3.get(0).get(
						"AVAILABLE_AMOUNT"));
			}
			if (amountList4.size() > 0) {
				act.setOutData("amountList4", amountList4.get(0).get(
						"AVAILABLE_AMOUNT"));
			}
			
			String type = "" ;
			if(capList.size()>0 ){
				type = capList.get(0).get("COST_TYPE").toString() ;
			}
			if(mediaList.size()>0){
				type = mediaList.get(0).get("COST_TYPE").toString() ;
			}
			act.setOutData("type", type) ;
			
			act.setOutData("mycheckList", mycheckList);
			act.setOutData("checkList", checkList);
			act.setOutData("targetMap", targetMap);
			act.setOutData("planMap", planMap);
			act.setOutData("attachmentList", attachmentList);
			act.setOutData("mediaList", mediaList);
			act.setOutData("capList", capList);
			act.setOutData("areaList", areaList);
			act.setOutData("groupName", groupName);
			act.setOutData("dealer",logonUser.getDealerId());
			act.setForword(addUrl);
		}catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"活动方案提报新增页面初始化");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
	
	/**
	 * 活动总结区域审批
	 */
	public void activitiesSummaryAreaCheck(){
		AclUserBean logonUser = (AclUserBean)act.getSession().get(Constant.LOGON_USER);
		try {
			RequestWrapper request = act.getRequest();
			String campaignId=request.getParamValue("campaignId")==null?"":request.getParamValue("campaignId");
			String executeId=request.getParamValue("executeId")==null?"":request.getParamValue("executeId");
			String flag=request.getParamValue("flag")==null?"":request.getParamValue("flag");
			//方案总结状态更新
			TtCampaignExecutePO execonpo=new TtCampaignExecutePO();
			execonpo.setExecuteId(new Long(executeId));
			TtCampaignExecutePO exepo=new TtCampaignExecutePO();
			
			if(null!=flag&&"0".equals(flag)){
				exepo.setCheckStatus(Constant.CAMPAIGN_CHECK_STATUS_09);
			}else{
				exepo.setCheckStatus(Constant.CAMPAIGN_CHECK_STATUS_08);
			}
			dao.update(execonpo, exepo);
			
			//添加审核明细信息
			String checkDesc=request.getParamValue("checkDesc");
			if(null==checkDesc){
				checkDesc="";
			}
			TtCampaignSummeryChkPO tcecp = new TtCampaignSummeryChkPO();
			String checkId =SequenceManager.getSequence("");
			tcecp.setCheckPositionId(logonUser.getPoseId()) ;
			tcecp.setCheckId(Long.parseLong(checkId));
			tcecp.setExecuteId(Long.parseLong(executeId));
			tcecp.setCheckOrgId(logonUser.getOrgId());
			tcecp.setCheckDate(new Date(System.currentTimeMillis()));
			tcecp.setCheckDesc(checkDesc);
			if("0".equals(flag)){
				tcecp.setCheckStatus(Constant.CAMPAIGN_CHECK_STATUS_09);
			}else{
				tcecp.setCheckStatus(Constant.CAMPAIGN_CHECK_STATUS_08);
			}
			tcecp.setCheckUserId(logonUser.getUserId());
			tcecp.setCreateBy(logonUser.getUserId());
			tcecp.setCreateDate(new Date(System.currentTimeMillis()));
			dao.insert(tcecp);
			//附件添加
			String []fjids = request.getParamValues("fjids");
			FileUploadManager.fileUploadByBusiness(checkId, fjids, logonUser);
			act.setOutData("returnValue", 1);
			
		}catch(Exception e) {//异常方法
			BizException e1 = new BizException(act,e,ErrorCodeConstant.QUERY_FAILURE_CODE,"活动方案总结提报");
			logger.error(logonUser,e1);
			act.setException(e1);
		}
	}
}
